/* -----------------------------------------------
 *       Filename:  rect.h
 *         Author:  MIEN
 *    Description:  矩形hash表
 *           Date:  2016-11-22
 * -----------------------------------------------
 */

#ifndef RECT_H
#define RECT_H

#ifndef uint
#define uint unsigned int
#endif

typedef union rect_key
{
    void *ptr;
    uint ord;
} rect_key_t;

typedef struct rect_node
{
    rect_key_t key;
    void *val;
    struct rect_node *next;
} rect_node_t;

typedef struct rect_table
{
    uint cols;
    uint rows;
    uint used;
    uint (*hash) (rect_key_t key);
    int (*compare) (rect_key_t key1, rect_key_t key2);
    void (*free) (void *val);
    rect_node_t ***ht;
} rect_table_t;

rect_table_t *rect_create();
rect_table_t *rect_create_with_size(int cols, int rows);
void  rect_free(rect_table_t *rt);
void *rect_get(rect_table_t *rt, rect_key_t key);
void  rect_set(rect_table_t *rt, rect_key_t key, void *val);
void  rect_del(rect_table_t *rt, rect_key_t key);

#define rect_capacity(r) ((r)->cols * (r)->rows)
#define rect_used(r) ((r)->used)
#define rect_size(r) ((r)->used)

#define RECT_DEFAULT_COLS 16
#define RECT_DEFAULT_ROWS 64

#endif /* RECT_H */